/*
*    Clean AJAX Engine v4.0
*    Copyright (C) 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/** Define debug mode (true or false) */
var env_Debug = false; 

/** Classes */
var env_Classes = ["classes.NanoEngine", "classes.Console", "classes.ParserTool"];

/** Clean real path (Assigned dynamically) */
var env_CleanPath = "";

/** User agent */
var env_UserAgent = navigator.userAgent.toLowerCase();

/**
* Get script real path
*/
function getPath(){
  var path = "";
  var scripts = document.getElementsByTagName('script');
  for(var i = 0; i < scripts.length; i++ ) {	  
    var src = scripts[i].getAttribute('src');
	if(src) {
	  var loader = src.match(/^(.*?)\/?clean-tiny.js/);
	  if(loader) {
	    var root = loader[0];
		path = root.substring(0, root.lastIndexOf("/") + 1);
		break;
	  }
    }
  }
  return path;
}

/**
* Import scripts to current page
*/
function importFile(jsFilePath){
  if(env_UserAgent.indexOf('safari') != -1)
	 document.writeln("<script type='text/javascript' src='../../../sw/etc/cleanajax/%22%20+%20jsFilePath%20+%20%22'></script>");	   
  else{
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = jsFilePath;
    head.appendChild(script);
  }
}

/**
* Import all scripts present in a package
*/
function importPackage(path, jsPackage){	
  for(var f in jsPackage){	  
    var file = jsPackage[f].replace(/\./g, '/') + '.js';
	importFile(path + file);
  }
}

/**
* Clean bootstrap code 
*/
function bootstrap(){
  env_CleanPath = getPath();	
  importPackage(env_CleanPath, env_Classes);
  main();    
}

/**
* Clean main code 
*/
function main(){	
  if(document.body)
    var engine = new NanoEngine(env_Debug);
  else
    setTimeout(main, 1000);
}

bootstrap();